<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="部门编号" prop="depId">
      <el-input v-model="dataForm.depId" placeholder="部门编号"></el-input>
    </el-form-item>
    <el-form-item label="姓名" prop="name">
      <el-input v-model="dataForm.name" placeholder="姓名"></el-input>
    </el-form-item>
    <el-form-item label="年龄" prop="age">
      <el-input v-model="dataForm.age" placeholder="年龄"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="gender">
      <el-input v-model="dataForm.gender" placeholder="性别"></el-input>
    </el-form-item>
    <el-form-item label="身份证号" prop="idcard">
      <el-input v-model="dataForm.idcard" placeholder="身份证号"></el-input>
    </el-form-item>
    <el-form-item label="学历" prop="academic">
      <el-input v-model="dataForm.academic" placeholder="学历"></el-input>
    </el-form-item>
    <el-form-item label="照片" prop="picture">
      <el-input v-model="dataForm.picture" placeholder="照片"></el-input>
    </el-form-item>
    <el-form-item label="住址" prop="address">
      <el-input v-model="dataForm.address" placeholder="住址"></el-input>
    </el-form-item>
    <el-form-item label="电话" prop="phone">
      <el-input v-model="dataForm.phone" placeholder="电话"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="state">
      <el-input v-model="dataForm.state" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createtime">
      <el-input v-model="dataForm.createtime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="updatetime">
      <el-input v-model="dataForm.updatetime" placeholder="修改时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          empId: 0,
          depId: '',
          name: '',
          age: '',
          gender: '',
          idcard: '',
          academic: '',
          picture: '',
          address: '',
          phone: '',
          state: '',
          createtime: '',
          updatetime: ''
        },
        dataRule: {
          depId: [
            { required: true, message: '部门编号不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '姓名不能为空', trigger: 'blur' }
          ],
          age: [
            { required: true, message: '年龄不能为空', trigger: 'blur' }
          ],
          gender: [
            { required: true, message: '性别不能为空', trigger: 'blur' }
          ],
          idcard: [
            { required: true, message: '身份证号不能为空', trigger: 'blur' }
          ],
          academic: [
            { required: true, message: '学历不能为空', trigger: 'blur' }
          ],
          picture: [
            { required: true, message: '照片不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '住址不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '电话不能为空', trigger: 'blur' }
          ],
          state: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          createtime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updatetime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.empId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.empId) {
            this.$http({
              url: this.$http.adornUrl(`/manager/employee/info/${this.dataForm.empId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.depId = data.employee.depId
                this.dataForm.name = data.employee.name
                this.dataForm.age = data.employee.age
                this.dataForm.gender = data.employee.gender
                this.dataForm.idcard = data.employee.idcard
                this.dataForm.academic = data.employee.academic
                this.dataForm.picture = data.employee.picture
                this.dataForm.address = data.employee.address
                this.dataForm.phone = data.employee.phone
                this.dataForm.state = data.employee.state
                this.dataForm.createtime = data.employee.createtime
                this.dataForm.updatetime = data.employee.updatetime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/manager/employee/${!this.dataForm.empId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'empId': this.dataForm.empId || undefined,
                'depId': this.dataForm.depId,
                'name': this.dataForm.name,
                'age': this.dataForm.age,
                'gender': this.dataForm.gender,
                'idcard': this.dataForm.idcard,
                'academic': this.dataForm.academic,
                'picture': this.dataForm.picture,
                'address': this.dataForm.address,
                'phone': this.dataForm.phone,
                'state': this.dataForm.state,
                'createtime': this.dataForm.createtime,
                'updatetime': this.dataForm.updatetime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
